package handler

import (
	"net/http"

	"github.com/gorilla/mux"

	"forum/pkg/repository/mysql"
)

func NewHandler() *mux.Router {
	r := mux.NewRouter()

	// set user apis
	userAPI := r.PathPrefix("/api/v1/user").Subrouter()
	{
		userHandler := initializeUserHandler(mysql.DB)
		userAPI.HandleFunc("/login", userHandler.Login).Methods(http.MethodPost)
		userAPI.HandleFunc("/logout", userHandler.Logout).Methods(http.MethodGet)
		userAPI.HandleFunc("/register", userHandler.Register).Methods(http.MethodPost)
		userAPI.HandleFunc("/reset", userHandler.ResetPassword).Methods(http.MethodPost)
	}

	// set profile apis
	profileAPI := r.PathPrefix("/api/v1/profile").Subrouter()
	memberAPI := r.PathPrefix(("/api/v1/member")).Subrouter()
	{
		profileHanlder := initializeProfileHandler(mysql.DB)
		profileAPI.HandleFunc("/settings", profileHanlder.GetProfileSettings).Methods(http.MethodGet)
		profileAPI.HandleFunc("/settings", profileHanlder.UpdateProfileSettings).Methods(http.MethodPut)
		memberAPI.HandleFunc("/{username}", profileHanlder.GetProfileSettingsByName).Methods(http.MethodGet)
	}

	return r
}
